<script setup>
import PageContainer from '@/components/pageContainer.vue'
import { useArticleCateStore } from '@/stores/articleCate.js'
import { ref } from 'vue'
import MyDialog from '@/components/channelDialog.vue'
import { deleteArticleCate } from '@/api/article.js'
const articleCateStore = useArticleCateStore()
const dialog = ref()
const loading = ref(true)



articleCateStore.getCateList().then(() => {
  loading.value = false
})
defineOptions({
  name: 'ArticleChannel'
})
// 新增分类
const addCate = async () => {
  // 调用组件暴露的方法
  dialog.value.open({})
}
// 编辑分类
const editCate = (row) => {
  dialog.value.open(row)

}
// 移除分类
const removeCate = async (id) => {
  console.log(id)
  await ElMessageBox.confirm('你确定要删除此类吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })

  await deleteArticleCate(id)
  ElMessage("删除成功！")
  // 删除过后，重新拿一次数据
  await articleCateStore.getCateList()

}

const onSuccess = () => {
  articleCateStore.getCateList()
}
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button @click="addCate">新增分类</el-button>
    </template>
    <my-dialog title="新增分类" ref="dialog" @success="onSuccess"></my-dialog>
    <el-table v-loading="loading" :data="articleCateStore.cateList" stripe style="width: 100%">
      <el-table-column type="index" label="Index" width="300" />
      <el-table-column prop="cate_name" label="分类名称"  />
      <el-table-column prop="cate_alias" label="分类别名" />
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="{ row }">
          <el-button link type="primary" @click="editCate(row)">
            编辑
          </el-button>
          <el-button link type="primary" @click="removeCate(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </page-container>
</template>

<style scoped>

</style>
